<template>
  <div class="login">
    <el-form ref="loginForm" :hide-required-asterisk="true" label-position="top" :model="loginForm" :rules="loginRules"
      class="login-form">
      <h3 class="title">欢迎登录 <span>文学炼金术</span></h3>
      <el-form-item prop="phone" label="账号" style="margin-bottom: 32px">
        <el-input v-model="loginForm.phone" type="text" auto-complete="off" placeholder="请输入手机号或账号ID">
        </el-input>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请登录输入密码"
          @keyup.enter.native="handleLogin">
        </el-input>
      </el-form-item>
      <div class="txBox">
        <el-checkbox v-model="loginForm.rememberMe">保持登录状态</el-checkbox>
        <span class="wjmm" @click="$router.push('/forget')">忘记密码？</span>
      </div>

      <el-form-item style="width:100%;">
        <el-button class="dlBtn" :loading="loading" size="medium" type="primary" style="width:100%;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="text-align: center" class="zc">
          还没有账号？点击<router-link class="link-type" :to="'/register'">注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!--    <div class="el-login-footer">-->
    <!--      <span>Copyright © 2018-2024 ruoyi.vip All Rights Reserved.</span>-->
    <!--    </div>-->
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data () {
    return {
      codeUrl: "",
      loginForm: {
        phone: "",
        password: "",
        rememberMe: false,
      },
      loginRules: {
        phone: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created () {
    this.getCookie();
  },
  methods: {

    getCookie () {
      const phone = Cookies.get("phone");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        phone: phone === undefined ? this.loginForm.phone : phone,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("phone", this.loginForm.phone, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("phone");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(() => { });
          }).catch(() => {
            this.loading = false;

          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;

}

.title {
  margin: 0px auto 48px auto;
  text-align: center;
  font-size: 32px;
  color: #000000;
  line-height: 38px;

  span {
    color: #FF7300;
    font-weight: bold;
    margin-left: 8px;
  }
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 500px;

  ::v-deep .el-form-item {
    margin-bottom: 16px;
  }

  ::v-deep .el-input {
    height: 48px !important;
    line-height: 48px !important;

    input {
      height: 48px;
      line-height: 48px !important;
      font-size: 16px;
    }
  }

  ::v-deep .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.login ::v-deep .el-form-item__label {
  font-size: 14px;
  color: #00011B;
  line-height: 16px;
  margin-bottom: 4px;
  font-weight: normal;
  padding: 0;
}

.txBox {
  font-size: 13px;
  color: rgba(0, 1, 27, 0.5);
  line-height: 15px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 48px;

  ::v-deep .el-checkbox__label {
    font-size: 13px;
    color: rgba(0, 1, 27, 0.5);
    font-weight: normal;
  }
}

.dlBtn {
  height: 54px;
  line-height: 54px;
  font-size: 18px;
  color: #FFFFFF;
  font-weight: bold;
  padding: 0;
}

.zc {
  font-size: 14px;
  color: #00011B;
  line-height: 16px;
  margin-top: 16px;
}
</style>
